import threading
import time,os

a = 100
lock = threading.Lock()
#函数方式
def add_thread():
    global a
    lock.acquire()
    a += 30
    time.sleep(1)
    print(f'a的值为{a}')
    lock.release()

def sub_thread():
    global a
    lock.acquire()
    a -= 50
    time.sleep(1)
    print(f'a的值为{a}')
    lock.release()
#继承方式
class add_thread1(threading.Thread):
    def run(self):
        global a
        lock.acquire()
        a += 40
        time.sleep(1)
        print(f'a的值为{a}')
        lock.release()
class sub_thread1(threading.Thread):
    def run(self):
        global a
        lock.acquire()
        a -= 10
        time.sleep(1)
        print(f'a的值为{a}')
        lock.release()

if __name__ == '__main__':
    print("主线程开始执行")
    print(f'a的值为{a}')
    ''' #函数方式示例
    thread1 = threading.Thread(target=add_thread)
    thread2 = threading.Thread(target=sub_thread)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    '''
    #继承方式
    my_thread1 = add_thread1()
    my_thread2 = sub_thread1()
    my_thread1.start()
    my_thread2.start()

    my_thread1.join()
    my_thread2.join()
    print(f'a的值为{a}')
    print("主线程执行完毕")